//
//  WBNetworkManager+Extension.swift
//  weibo
//
//  Created by wenquan wang on 12/2/16.
//  Copyright © 2016 wenquan wang. All rights reserved.
//

import Foundation
/// MARK: -sian weibo
extension WBNetworkManager{
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion: @escaping ( (List: [[String: AnyObject]]?, isSuccess: Bool) )->()){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parames = ["since_id": "\(since_id)",
            "max_id": "\(max_id > 0 ? max_id-1: 0)"]
        
        tokenRequest(method: .GET, urlString: urlString, parameters: parames as [String : AnyObject]?){
            (json:AnyObject?, isSuccess:Bool) in
            let result = json?["statuses"] as? [[String: AnyObject]]
            
            completion((list: result, isSuccess: isSuccess))
        }

    }
    
    func unreadCount(completion: @escaping ( (count: Int) ) -> () ) {
        guard let uid = userAccount.uid else{
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid": uid]
        
        tokenRequest(urlString: urlString, parameters: params as [String : AnyObject]?) {
            (json:AnyObject?, isSuccess:Bool) in
            
            let dict = json as? [String: AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
            
        }
        
    }
    
}
extension WBNetworkManager{
    func loadUserInfo(completion: @escaping ((dict: [String: AnyObject])) -> ()) {
        guard let uid = userAccount.uid else{
            return
        }
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid": uid]
        
        tokenRequest(urlString: urlString, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            completion((json as? [String: AnyObject]) ?? [:])
        }
    }
}
extension WBNetworkManager{
    /// 提问：网络请求异步到底应该返回什么？-需要什么返回什么？
    /// 加载 AccessToken
    ///
    /// - parameter code:       授权码
    /// - parameter completion: 完成回调[是否成功]
    func loadAccessToken(code: String, completion: @escaping( (isSuccess: Bool) ) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id": WBAppKey,
                      "client_secret": WBAppSecret,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": WBRedirectURI]
        
        // 发起网络请求
        request(method: .POST, urlString: urlString, parameters: params as [String : AnyObject]){ (json, isSuccess) in
            self.userAccount.yy_modelSet(withJSON: json as? [String: AnyObject] ?? [:])
            print(self.userAccount)
            
            
            self.loadUserInfo(completion: { (dict) in
                print(dict)
                self.userAccount.yy_modelSet(with: dict)
                self.userAccount.saveAccount()
                print(self.userAccount)
            })
            
            completion(isSuccess)
        }
    }
}
